package org.jeecg.modules.crm.nei.service.impl;

import org.jeecg.modules.crm.nei.entity.NeiAaOrder;
import org.jeecg.modules.crm.nei.entity.NeiAaOrderProduct;
import org.jeecg.modules.crm.nei.entity.NeiAaOrderAddress;
import org.jeecg.modules.crm.nei.mapper.NeiAaOrderProductMapper;
import org.jeecg.modules.crm.nei.mapper.NeiAaOrderAddressMapper;
import org.jeecg.modules.crm.nei.mapper.NeiAaOrderMapper;
import org.jeecg.modules.crm.nei.service.INeiAaOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 水果订单表
 * @Author: jeecg-boot
 * @Date:   2024-07-31
 * @Version: V1.0
 */
@Service
public class NeiAaOrderServiceImpl extends ServiceImpl<NeiAaOrderMapper, NeiAaOrder> implements INeiAaOrderService {

	@Autowired
	private NeiAaOrderMapper neiAaOrderMapper;
	@Autowired
	private NeiAaOrderProductMapper neiAaOrderProductMapper;
	@Autowired
	private NeiAaOrderAddressMapper neiAaOrderAddressMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(NeiAaOrder neiAaOrder, List<NeiAaOrderProduct> neiAaOrderProductList,List<NeiAaOrderAddress> neiAaOrderAddressList) {
		neiAaOrderMapper.insert(neiAaOrder);
		if(neiAaOrderProductList!=null && neiAaOrderProductList.size()>0) {
			for(NeiAaOrderProduct entity:neiAaOrderProductList) {
				//外键设置
				entity.setOrderId(neiAaOrder.getId());
				neiAaOrderProductMapper.insert(entity);
			}
		}
		if(neiAaOrderAddressList!=null && neiAaOrderAddressList.size()>0) {
			for(NeiAaOrderAddress entity:neiAaOrderAddressList) {
				//外键设置
				entity.setOrderId(neiAaOrder.getId());
				neiAaOrderAddressMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(NeiAaOrder neiAaOrder,List<NeiAaOrderProduct> neiAaOrderProductList,List<NeiAaOrderAddress> neiAaOrderAddressList) {
		neiAaOrderMapper.updateById(neiAaOrder);
		
		//1.先删除子表数据
		neiAaOrderProductMapper.deleteByMainId(neiAaOrder.getId());
		neiAaOrderAddressMapper.deleteByMainId(neiAaOrder.getId());
		
		//2.子表数据重新插入
		if(neiAaOrderProductList!=null && neiAaOrderProductList.size()>0) {
			for(NeiAaOrderProduct entity:neiAaOrderProductList) {
				//外键设置
				entity.setOrderId(neiAaOrder.getId());
				neiAaOrderProductMapper.insert(entity);
			}
		}
		if(neiAaOrderAddressList!=null && neiAaOrderAddressList.size()>0) {
			for(NeiAaOrderAddress entity:neiAaOrderAddressList) {
				//外键设置
				entity.setOrderId(neiAaOrder.getId());
				neiAaOrderAddressMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		neiAaOrderProductMapper.deleteByMainId(id);
		neiAaOrderAddressMapper.deleteByMainId(id);
		neiAaOrderMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			neiAaOrderProductMapper.deleteByMainId(id.toString());
			neiAaOrderAddressMapper.deleteByMainId(id.toString());
			neiAaOrderMapper.deleteById(id);
		}
	}
	
}
